package com.example.android.ui.hb;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.android.R;
import com.example.android.bean.BaseBean;
import com.example.android.bean.Goods;
import com.example.android.ui.addHB.AddHBActivity;
import com.example.android.ui.base.BaseFragment;
import com.example.android.ui.hb.Adapter.HBRecyclerViewAdapter;
import com.example.android.ui.hb.Adapter.HBSpanSizeLookup;
import com.example.android.ui.hb.detail.HBDetailActivity;
import com.example.android.ui.search.SearchActivity;
import com.example.android.ui.searchHB.SearchHBActivity;

import java.util.ArrayList;
import java.util.List;

public class HBFragment extends BaseFragment implements View.OnClickListener, HBContract.IHBView, SwipeRefreshLayout.OnRefreshListener, HBRecyclerViewAdapter.OnItemClickListener {
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private HBRecyclerViewAdapter hbRecyclerViewAdapter;
    private HBSpanSizeLookup hbSpanSizeLookup;
    private HBPresenter hbPresenter;
    private EditText search;
    private ImageView add;
    private PopupWindow mPopWindow;
    @Override
    protected void initViews() {
        swipeRefreshLayout = find(R.id.hb_swiperefresh);
        recyclerView =find(R.id.hb_recycler);
        search = find(R.id.hb_search);
        add =find(R.id.hb_image_add);

        //监听事件
        swipeRefreshLayout.setOnRefreshListener(this);
        search.setOnClickListener(this);
        add.setOnClickListener(this);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),4);
        List<Goods> HBList = new ArrayList<>();
        hbSpanSizeLookup = new HBSpanSizeLookup(HBList);
        gridLayoutManager.setSpanSizeLookup(hbSpanSizeLookup);
        recyclerView.setLayoutManager(gridLayoutManager);
        hbRecyclerViewAdapter = new HBRecyclerViewAdapter(recyclerView,getActivity(),HBList);
        hbRecyclerViewAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(hbRecyclerViewAdapter);

        hbPresenter = new HBPresenter(this);
        hbPresenter.getHbList();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_hb;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.hb_search:
                Intent intent = new Intent(getActivity(), SearchHBActivity.class);
                startActivity(intent);
                break;
            case R.id.hb_image_add:
                showPopupWindow();
                break;
            case R.id.pop_add_goods:
                break;
            case R.id.pop_add_hb:
                Intent intent1 = new Intent(getActivity(), AddHBActivity.class);
                startActivity(intent1);
                break;
            default:
                break;
        }
    }

    @Override
    public void getHBListSuccess(BaseBean<List<Goods>> HBList) {
        hbSpanSizeLookup.setHBList(HBList.getData());
        hbRecyclerViewAdapter.setHBList(HBList.getData());
        Log.i("HBFragment","数据条数："+HBList.getData().size());
    }

    @Override
    public void getHBListError(Throwable throwable) {
        Log.e("HBFragment",throwable.toString());
    }

    @Override
    public void onRefresh() {
        hbPresenter.getHbList();
        swipeRefreshLayout.setRefreshing(false);
    }
    public void showPopupWindow(){
        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.pop_window,null);
        mPopWindow = new PopupWindow(contentView,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);
        mPopWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.setFocusable(true);
        mPopWindow.setAnimationStyle(R.style.pop_add);
        TextView add_goods = contentView.findViewById(R.id.pop_add_goods);
        TextView add_hb = contentView.findViewById(R.id.pop_add_hb);
        add_hb.setOnClickListener(this);
        add_goods.setOnClickListener(this);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.activity_main,null);
        mPopWindow.showAsDropDown(add,-150,10);
    }

    @Override
    public void onItemClick(Goods goods) {
        Intent intent = new Intent(getActivity(), HBDetailActivity.class);
        intent.putExtra("hbId",goods.getGoodsId());
        startActivity(intent);
    }
}
